package com.brother.util;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.brother.database.MalaysiaAddressDB;
import com.brother.model.LatLngPoint;
import com.brother.service.RealDataService;

public class MalaysiaAddress {
private static Logger log = Logger.getLogger(RealDataService.class);
private static 	MalaysiaAddress instance =null;

private List<LatLngPoint> bounds ;


private MalaysiaAddress(){
	
	// lng    lat   lng     lat
	// 99.612,0.854,119.926,7.6253333333333355
	bounds = new ArrayList<LatLngPoint>();
	LatLngPoint p = new LatLngPoint(0.854f, 99.612f);
	bounds.add(p);
	p = new LatLngPoint(0.854f, 119.926f);
	bounds.add(p);
	p = new LatLngPoint(7.6253333333333355f, 119.926f);
	bounds.add(p);
	p = new LatLngPoint(7.6253333333333355f, 99.612f);
	bounds.add(p);
}

public static MalaysiaAddress getInstance(){
	if(instance == null){
		instance = new MalaysiaAddress();
	}
	return instance;
}

public boolean isIntesect(float lat, float lng){
	return PointIntersect.isInsidePolygon(bounds, new LatLngPoint(lat, lng));
}


public String getAddressByDB(float lat, float lng){
	if(!this.isIntesect(lat, lng)){
		return  null;
	}
	String sql = "select address from malaysia_point where (lat between "+(lat-0.0005)+"  and "+(lat+0.0005)+
	") and (lng between "+(lng-0.0005)+" and "+(lng+0.0005)+") ";
//	System.out.println(sql);
	ResultSet rs = MalaysiaAddressDB.query(sql );
	String address = "";
	try {
		while(rs.next()){
			if(rs.getString("address") == null || rs.getString("address").trim().length() == 0){
				continue;
			}
			address = rs.getString("address");
		}
	} catch (SQLException e) {
		log.error("", e);
	}
	sql = "select address from malaysia_polyline where" +
			" Intersects(GeomFromText('MULTILINESTRING(("+lng+" "+(lat-0.0005)+", "+lng+" "+(lat+0.0005)+"))'),p_line)=1";
	rs = MalaysiaAddressDB.query(sql);
//	System.out.println(sql);
	try {
		while(rs.next()){
			if(rs.getString("address") == null || rs.getString("address").trim().length() == 0){
				continue;
			}
			if(address.length() > 0){
				address = address + ", " +rs.getString("address");
			}else{
				address = rs.getString("address");
			}
			break;
		}
	} catch (SQLException e) {
		log.error("", e);
	}
	sql = "select address from `malaysia_region` where" +
	" Within(GeomFromText('point("+lng+" "+lat+"))'),`region`)=1";
	rs = MalaysiaAddressDB.query(sql);
//	System.out.println(sql);
	try {
		while(rs.next()){
			if(rs.getString("address") == null || rs.getString("address").trim().length() == 0){
				continue;
			}
			if(address.length() > 0){
				address = address + ", " +rs.getString("address");
			}else{
				address = rs.getString("address");
			}
			break;
		}
		if(address.length() == 0){
			address = "Malaysia";
		}else{
			address = address+", Malaysia";
		}
	} catch (SQLException e) {
		log.error("", e);
	}
	MalaysiaAddressDB.closeThreadConnection();
	return address;
}



	
}
